package com.sgb.goods.view.adapter.publish;

import androidx.databinding.ViewDataBinding;

import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsItemGoodsTypeBinding;
import com.sgb.goods.entity.MenuEntity1;
import com.sgb.goods.utils.StringUtils;
import com.sgb.goods.utils.base.adapter.RecordPointCommonAdapter;
import com.swgk.core.util.MLog;

/**
 * 商品类型选择右侧适配器
 * @author cyj
 */
public class GoodsTypeRightAdapter extends RecordPointCommonAdapter<MenuEntity1<MenuEntity1>> {

    public GoodsTypeRightAdapter() {
        super(R.layout.goods_item_goods_type, BR.viewModel);
    }

    @Override
    protected void convert(BaseViewHolder baseHolder, ViewDataBinding dataBinding, MenuEntity1<MenuEntity1> entity) {
        GoodsItemGoodsTypeBinding mDataBinding = (GoodsItemGoodsTypeBinding) dataBinding;
        RecordPointCommonAdapter<MenuEntity1> adapter = new RecordPointCommonAdapter<MenuEntity1>
                (R.layout.goods_item_goods_type_text, entity.getDatas(), BR.viewModel) {
            @Override
            public boolean equivalentToObject(MenuEntity1 object1, MenuEntity1 object2) {
                if(null == object1 || null == object2) {
                    return false;
                }
                return !StringUtils.isEmpty(object1.id) && object1.id.equals(object2.id);
            }
        };
        mDataBinding.rlvList.setAdapter(adapter);
        adapter.setRecordPoint(null == currentObj || -1 == oldPosition ? null : currentObj.getDatas().get(oldPosition));
        adapter.setItemClickListener((adapter2, view, position2) -> {
            // 上一个非自身列表的选中项设置未选中
            if(null != currentObj && !currentObj.equals(entity)) {
                currentObj.getDatas().get(position).isSelectIt.set(false);
            }
            // 二级数据赋值
            currentObj = entity;
            position = position2;
            MLog.e("当前选中的cid2及参数：", "name:" +entity.name + " | cid2: " + entity.id);
            MLog.e("当前选中的cid3及参数：", "name:" + adapter.getData().get(position).name + " | cid3: " + adapter.getData().get(position).id);
        });
    }

    @Override
    public boolean equivalentToObject(MenuEntity1<MenuEntity1> object1, MenuEntity1<MenuEntity1> object2) {
        // 由于此处的二级数据无需呈现点击效果，则返回false不处理
        return false;
    }

    @Override
    public void reset(boolean isResetAll) {
        if(null != currentObj && null != currentObj.getDatas() && null != currentObj.getDatas().get(position)) {
            currentObj.getDatas().get(position).isSelectIt.set(false);
        }
        super.reset(isResetAll);
    }
}
